<template>
  <div>
    <!-- 顶部栏 -->
    <van-nav-bar
      class="my-nav-bar"
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="$router.push('/')"
    />
    <van-form @submit="onSubmit">
    <!-- 输入框表单 -->
    <!-- 输入手机号，调起手机号键盘 -->
    <van-field 
    v-model="mobile" 
    required type="tel" 
    label="手机号"  
    placeholder="请输入手机号"
    name="mobile"
    :rules="[
    { required: true, message: '请填写手机号' },
    {
        pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
        message: '手机号格式不对'
    }
    ]"
    />
    <!-- 输入密码 -->
    <van-field 
    v-model="code" 
    required label="验证码" 
    placeholder="请输入验证码"
    name="code"
    :rules="[
    { required: true, message: '请填写验证码' },
    { pattern: /^\d{6}$/, message: '验证码格式不对' }
    ]"
    />
     <div style="margin: 16px;">
    <van-button 
    round 
    block 
    type="info" 
    native-type="submit"
    :disabled="isLoading"
    :loading="isLoading" 
    loading-text="登录中..."
    >提交</van-button>
  </div>
    </van-form>
  </div>
</template>

<script>
import {getLogin} from './user.js'
import {saveToken} from '@/utils/token.js'
export default {
  name: 'login',
  data() {
      return {
          mobile:'13212345678',
          code:'246810',
          isLoading:false,
      }
  },
  methods: {
      onSubmit(values){
          //values是个对象
          //values中对象的属性名是:表单的name值,属性值是:表单value
          // console.log('submit',values);
          this.isLoading=true
          getLogin(values)
          .then(res=>{
              this.$toast.success('登录成功')
              saveToken(res.data.data)
              this.$router.push({path:'/user'})
          }).catch(err => {
            this.$toast.fail('账号或验证码错误')
          }).then(()=>{
            setTimeout(() => {
              this.isLoading=false
            }, 800);
          })
      }
  },
}
</script>

<style lang="less">
.my-nav-bar {
  &.van-nav-bar {
    background-color: #3196fa;
  }
  .van-nav-bar__title {
    color: #fff;
  }
  .van-nav-bar__left {
    .van-icon-arrow-left,
    .van-nav-bar__text {
      color: #fff;
    }
  }
}
</style>
